package com.bianguo.android.tencentpushdemo.view.widget.behaviors;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;

import com.bianguo.android.tencentpushdemo.utils.MyLog;

/**
 * Created by Administrator on 2017/5/26.
 */

public class BottomBarBehavior extends CoordinatorLayout.Behavior<View> {

    private int statusBarHeight;

    public BottomBarBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获取status_bar_height资源的ID
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }
    }

    //确定所提供的子视图是否有另一个特定的同级视图作为布局从属。
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        //这个方法是说明这个子控件是依赖AppBarLayout的
        return dependency instanceof AppBarLayout;
    }

    //用于响应从属布局的变化
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {

        float translationY = Math.abs(dependency.getTop() - statusBarHeight);//获取更随布局的顶部位置

        child.setTranslationY(translationY);
        return true;
    }
}
